package com.lianxueda.drawing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.domain.dto.ClassFormDTO;
import com.lianxueda.drawing.domain.dto.ClassQueryDTO;

import com.lianxueda.drawing.domain.pojo.Classes;
import com.lianxueda.drawing.domain.pojo.Courses;
import com.lianxueda.drawing.domain.pojo.Students;
import com.lianxueda.drawing.domain.vo.ClassStudentVO;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.domain.vo.WatchStudentVO;
import com.lianxueda.drawing.mapper.ClassMapper;
import com.lianxueda.drawing.mapper.WxUserMapper;
import com.lianxueda.drawing.service.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author 29264
 */

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/v1/class")
@Tag(name = "班级管理模块", description = "班级管理模块接口")
@RestController
@RequiredArgsConstructor
public class ClassController {

    @Resource
    private ClassService classService;
    @Resource
    private ClassMapper classMapper;

    @Resource
    private StudentsService studentService;
    @Resource
    private CoursesService courseService;

    @Resource
    private ClassCoursesService classCoursesService;

    @Resource
    private CourseStudentService courseStudentService;

    @Resource
    private WxUserMapper userMapper;

    //新增班级
    @Operation(summary="新增班级")
    @PostMapping("/add")
    public R addClass(@RequestBody ClassFormDTO classForm) {


        if (classService.getOne(new QueryWrapper<Classes>().eq("name", classForm.getName())) != null) {
            return R.fail("班级已经存在！");
        }
        Classes classes = new Classes();
        classes.setName(classForm.getName());
//        classes.setTeacherId(classMapper.selectTeacherIdByName(classForm.getTeacherName()));
//        System.out.println(classMapper.selectTeacherIdByName(classForm.getTeacherName())+"ddddddddddddd");
        //将字符串时间转换为date类型
        // 将字符串时间转换为 LocalDateTime 类型
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDateTime startTime = LocalDateTime.parse(classForm.getStartTime(), formatter);
//        classes.setStartTime(startTime);

        // 将字符串时间转换为 LocalDate 类型
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(classForm.getStartTime(), formatter);
        // 如果你的 Classes 类使用 LocalDate 而不是 LocalDateTime
        classes.setStartTime(startDate);

        classService.save(classes);
        return R.suc("班级添加成功！");
    }

    //删除班级
    @Operation(summary="删除班级")
    @DeleteMapping("/delete")
    public R deleteClass(@RequestParam Integer id) {
        if (classService.getById(id) == null) {
            return R.fail("班级不存在！");
        }
        classService.removeById(id);
        return R.suc("班级删除成功！");
    }

    //修改班级
    @Operation(summary="修改班级")
    @PutMapping("/update")
    public R updateClass(@RequestBody ClassFormDTO classForm) {
        Integer classId = classForm.getId();
        if (classId == null) {
            return R.fail("班级不存在！");
        }
        Classes classes = new Classes();
        classes.setName(classForm.getName());
//        classes.setTeacherId(classMapper.selectTeacherIdByName(classForm.getTeacherName()));

        // 将字符串时间转换为 LocalDate 类型
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(classForm.getStartTime(), formatter);
        classes.setStartTime(startDate);

        classMapper.updateByClassId(classForm);
        return R.suc("班级修改成功！");
    }


    @PostMapping("/Pagelist")
    @Operation(summary = "分页查询班级")
    public R pageList(@RequestBody ClassQueryDTO classQueryDTO){
        PageVO pageVo = classService.searchPage(classQueryDTO);
        return R.suc(pageVo);
    }



    @GetMapping("/byClass/{classId}")
    @Operation(summary = "根据班级id查询学生")
    public ClassStudentVO getStudentsByClassId(@PathVariable Integer classId) {
        return studentService.getStudentsByClassId(classId);
    }

    //根据class_id在class_courses表中查询班级course_id,根据course_id在courses表中查询课程对象
    @GetMapping("/getCourse/{classId}")
    @Operation(summary = "根据班级id查询课程对象")
    public List<Courses> getCourse(@RequestParam Integer classId) {
       return courseService.selectCourseById(classId);

    }

    //列出班级名称
    @GetMapping("/getClassName")
    @Operation(summary = "列出班级名称")
    public R getClassName() {
        List<String> classNameList = classService.getClassNameList();
        return R.suc(classNameList);
    }





    //返回学生信息
    @GetMapping("/getStudent")
    @Operation(summary = "通过班级id返回学生信息")
    public R getStudent(@RequestParam Integer classId) {
        List<WatchStudentVO> ws = new ArrayList<>();

        //根据班级id查找课程id列表
        List<Integer> courseIds = classCoursesService.selectCourseIdByClassId(classId);

        //如果找不到课程id列表，返回空列表
        if (courseIds.isEmpty()) {
            return R.fail("班级学生为空");
        }
        //根据课程id列表在course_student表中查找学生id列表
        List<Integer> studentIds = courseStudentService.selectStudentsByCourseIds(courseIds);


        //根据学生id查找学生信息
        List<Students> students = studentService.listByIds(studentIds);


        //根据父母手机号在user表中查找nickname
        for (Students student : students) {
            WatchStudentVO watchStudentVO = new WatchStudentVO();
            String parentPhone = student.getParentPhone();
            String nickname = userMapper.selectNicknameByPhone(parentPhone);
            watchStudentVO.setParentName(nickname);
            watchStudentVO.setParentPhone(parentPhone);
            watchStudentVO.setStudentName(student.getName());
            ws.add(watchStudentVO);
        }

        return R.suc(ws);
    }




}
